<?php
/**
 * @file bg1.php
 * @package taobaoke
 * @subpackage 
 * @version $id: 0.1 UTF-8 2009-7-23 下午01:26:13 $
 * @author LD King (mifunny.info)
 * @copyright Copyright (c) 2007 - 2009 MiFunny China Inc. (http://mifunny.info)
 * 淘宝客商品、商铺入库
 */
header("Content-Type:text/html;charset=UTF-8");
require_once 'lib/functions.php';
/*($session, $params=array('cid' => '16', //cid分类id 默认分类：女装/女士精品
													'keyword' => '雪纺', //keyword关键字，默认： 
													'sort' => 'default', //默认排序:default , 信用等级从高到低:credit_desc 
													'page_size' => 35)){*/
$action = $_POST['action'];
$session = $_POST['session'];
$cid = $_POST['cid'];
$keyword = $_POST['keyword'];
$sort = $_POST['sort'];
$page_size = $_POST['page_size'];

if ($action == 'getTaobaoke'){
	echo "<?xml version='1.0' encoding='utf-8'?>".
		"<items>";
	
	$params = array('cid' => $cid,
					'keyword' => $keyword,
					'sort' => $sort,
					'page_size' => $page_size);
	
	$dbh = new tbkDB();
	$item = getTaobaokeItems($session, $params);
	for($i=0; $i<count($item); $i++){

		if ($dbh->isExist($item[$i]['iid'], 'items'))
			continue;

		$item2 = getTaobaoItem($session, $item[$i]['nick'],$item[$i]['iid']);
		$itemArray = array(
			'iid' => $item[$i]['iid'],
			'title' => $item[$i]['title'],
			'pic_url' => $item[$i]['pic_url'],
			'price' => $item[$i]['price'],
			'click_url' => $item[$i]['click_url'],
			'nick' => $item[$i]['nick'],
			'commission' => $item[$i]['commission'],
			'commission_rate' => $item[$i]['commission_rate'],
			'commission_num' => $item[$i]['commission_num'],
			'commission_volume' => $item[$i]['commission_volume'],
			'cid' => $item2['cid'],
			'title2' => $item2['title2'],
			'location' => $item2['location'],
			'properties' => $item2['properties'],
			'desc' => $item2['desc']
		);
		if ($i == 0) $is = true; //热门商品
		else $is = false;
		//$dbh->inItem($itemArray, $is);
		//echo "<title>".$item[$i]['title']."</title>";
		
		$shop = getTaobaokeShop($session,$item[$i]['nick']);
		if ($dbh->isExist($shop['user_id'],'shops')){ //店家是否已经入库
			$dbh->inItem($itemArray, $is);
			echo "<item><title>".$item[$i]['title']."</title></item>";
			continue;
		}elseif ( empty($shop['click_url']) or empty($item2['title2']) ){ //店家信息不全
			continue;
		}
		
		$shopArray = array(
			'user_id' => $shop['user_id'],
			'nick' => $shop['nick'],
			'credit' => $shop['credit'],
			'sex' => $shop['sex'],
			'sid' => $shop['sid'],
			'title' => $shop['title'],
			'desc' => $shop['desc'],
			'created' => $shop['created'],
			'click_url' => $shop['click_url'],
			'shop_commission.rate' => $shop['shop_commission.rate']
		);
		
		$dbh->inItem($itemArray, $is);
		$dbh->inShop($shopArray);
		echo "<item><title>".$item[$i]['title']."</title><shop>".$shop['nick']."</shop></item>";
	} //for($i=0; $i<count($item); $i++)

	echo '</items>';
} //if ($action == 'getTaobaoke')
?>